package org.prail.ui.utils;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.EventObject;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

import javazoom.jl.player.Player;

import org.prail.net.core.Stream;
import org.prail.threads.StreamThread;
import org.prail.ui.ResultsTable;
import org.prail.wrappers.SearchResult;

public class CustomButton extends JButton implements TableCellRenderer,
		TableCellEditor {
	private static final long serialVersionUID = 7375765370176642596L;
	private static int selectedRow;
	private static int selectedColumn;
	public static ArrayList<JButton> buttons = new ArrayList<JButton>();

	public CustomButton(String text) {
		super(text);
		setOpaque(true);
		buttons.add(this);
		addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(getText().equals("Stop")) {
					final Player player = Stream.getPlayer();
					if(player != null) {
						player.close();
					}
					setText("Play");
					return;
				}
				for(final JButton button : buttons) {
					if(!button.getText().equals("Play")) {
						button.setText("Play");
					}
				}
				DefaultTableModel m = (DefaultTableModel) ResultsTable.get()
						.getModel();
				m.fireTableDataChanged();
				if (getText().equals("Play")) {
					final SearchResult selected = ResultsTable.tableList
							.get(selectedRow);
					System.out.println("Playing: " + selected.toString() + "["
							+ selectedRow + "]");
					new StreamThread(selected.getMP3Url()).start();
					setText("Stop");
				}
			}
		});
	}

	public int getSelectedRow() {
		return selectedRow;
	}

	public int getSelectedColumn() {
		return selectedColumn;
	}

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		if (value instanceof JComponent) {
			return (JComponent) value;
		} else {
			return null;
		}
	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int col) {
		selectedRow = row;
		selectedColumn = col;
		System.out.println(row);
		if (value instanceof JComponent) {
			return (JComponent) value;
		} else {
			return null;
		}
	}

	@Override
	public void addCellEditorListener(CellEditorListener arg0) {
	}

	@Override
	public void cancelCellEditing() {
	}

	@Override
	public Object getCellEditorValue() {
		return "";
	}

	@Override
	public boolean isCellEditable(EventObject arg0) {
		return true;
	}

	@Override
	public void removeCellEditorListener(CellEditorListener arg0) {
	}

	@Override
	public boolean shouldSelectCell(EventObject arg0) {
		return true;
	}

	@Override
	public boolean stopCellEditing() {
		return true;
	}

}
